﻿using System;
using System.Configuration;

namespace Raffles.OA.Utility
{
    /// <summary>
    /// 配置文件中属性的读取类
    /// </summary>
    public class ConfigHelper
    {
        /// <summary>
        /// 根据Key取Value值
        /// </summary>
        /// <param name="key"></param>
        public static string GetValue(string key)
        {
            return ConfigurationManager.AppSettings[key].ToString().Trim();
        }

        /// <summary>
        /// 根据Key取Value值
        /// </summary>
        /// <param name="key"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static int GetIntValue(string key, int defaultValue = int.MaxValue)
        {
            int value;
            if (int.TryParse(ConfigurationManager.AppSettings[key].Trim(), out value))
            {
                return value;
            }

            return defaultValue;
        }

        /// <summary>
        /// 根据Key取Value值
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static DateTime GetDateTimeValue(string key)
        {
            DateTime value = new DateTime(2019, 1, 1);
            DateTime.TryParse(ConfigurationManager.AppSettings[key].Trim(), out value);
            return value;
        }

        /// <summary>
        /// 根据Key取Value值
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static byte GetByteValue(string key)
        {
            byte value = byte.MinValue;
            byte.TryParse(ConfigurationManager.AppSettings[key].Trim(), out value);
            return value;
        }
    }
}
